import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class PPButtonRowView extends StatefulWidget {
	
	PPButtonRowView({Key key, this.data, this.action}) : super(key: key);

	final List<Map> data;
	final Function action;

	@override
	_PPButtonRowViewState createState() => _PPButtonRowViewState();
}

class _PPButtonRowViewState extends State<PPButtonRowView> {
	
	@override
	Widget build(BuildContext context) {
		return Row(
					key: new Key('PP_First_Tab_Row'), 
					children: _getRowChildrenWidget()
				);
	}

	_getRowChildrenWidget() {
		List<Widget> list = [];
		if(widget.data != null && widget.data.length > 0) {
			widget.data.forEach((item) {
				list.add(_getFlatButton(item, widget.data.indexOf(item)));
			});
		}
		return list;
	}

	_getFlatButton(item, index) {
		return (FlatButton(
				child: Container(
				padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
				decoration: _getBoxDecoration(item['isSelect']),
					child: Text(item['title'].toString(),
						style: _getTextStyle(item['isSelect']),
						textAlign: TextAlign.center),
					),					
					onPressed: () { _onPressButtonActon(index); },
				));
	}

	_getBoxDecoration(isSelect) {
		if(isSelect) {
			return BoxDecoration(color: Colors.cyan, border: new Border.all(color: Colors.cyan, width: 1));
		} else {
			return BoxDecoration(border: new Border.all(color: Colors.grey, width: 1));
		}
	}

	_getTextStyle(isSelect) {
		if(isSelect) {
			return TextStyle(fontSize: 14, color: Colors.white);
		} else {
			return TextStyle(fontSize: 14, color: Colors.grey);
		}
	}

	_onPressButtonActon(index) {
		if(widget.action != null) {
			widget.action(index);
		}
	}
}
